﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Xna.Framework;

namespace SkyPoint
{
    public class Utils
    {
        public static double ConvertRaToLongitude(double rA)
        {
            double deg = rA * 15;
            return deg - 180;
        }

        public static double ArcDistance(Coord p1, Coord p2)
        {
            double long1 = Utils.ConvertRaToLongitude(p1.RightAscension) * Math.PI / 180;
            double long2 = Utils.ConvertRaToLongitude(p2.RightAscension) * Math.PI / 180;
            double longDiff = long2 - long1;
            double lat1 = p1.Declination * Math.PI / 180;
            double lat2 = p2.Declination * Math.PI / 180;
            double latDiff = lat2 - lat1;
            double h = Math.Pow(Math.Sin(latDiff / 2), 2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Pow(Math.Sin(longDiff / 2), 2);
            return 2 * Math.Atan2(Math.Sqrt(h), Math.Sqrt(1 - h));
        }

        public static Vector3 ConvertSphericalToCartesian(Coord coordinate)
        {
            Vector3 v = new Vector3();

            double phi = (90 - coordinate.Declination) * Math.PI / 180;
            double theta = coordinate.RightAscension * 15 * Math.PI / 180;
            v.X = (float)(Math.Cos(theta) * Math.Sin(phi));
            v.Y = (float)(Math.Sin(theta) * Math.Sin(phi));
            v.Z = (float)(Math.Cos(phi));
            
            return v;
        }
    }
}
